package com.conversationboard.view;

import java.sql.SQLException;

import com.conversationboard.config.Configuration;
import com.conversationboard.model.Bookmark;
import com.conversationboard.model.Bookmarks;

public class BookmarksDisplay {

	public static String display(Bookmarks bookmarks, boolean displayDeletionInfo) throws SQLException {
		return display(bookmarks, displayDeletionInfo, 0, 0);
	}


	public static String display(Bookmarks bookmarks, boolean displayDeletionInfo, int boardId, int threadId) throws SQLException {

		StringBuilder buffer = new StringBuilder();

		boolean anyUpdated = false;

		for (Bookmark bookmark : bookmarks) {
			if (bookmark.isUpdated()) {
				anyUpdated = true;
				break;
			}
		}

		if ((!anyUpdated) && (!displayDeletionInfo)) {
			return buffer.toString();
		}

		int bookmarksWithUpdates = 0;

		buffer.append("<div class=\"bookmarks\">");

		if (displayDeletionInfo) {
			if (!bookmarks.isEmpty()) {
				buffer.append("<p><a href=" + Configuration.getInstance().getRoot() + "/Pages/confirmbookmarkdeleteall.jsp>Delete All My Bookmarks</a></p>");
			}
			
			buffer.append("<form action=\"" + Configuration.getInstance().getRoot() + "/ConfirmDeleteSelectedBookmarksControllerServlet\" method=\"post\">");
		}

		for (Bookmark bookmark : bookmarks) {

			if ((!displayDeletionInfo) && (!bookmark.isUpdated())) {
				continue;
			}

			boolean thisThreadsBookmark = false;

			if ((boardId == bookmark.getBoardId() && threadId == bookmark.getThreadId())) {
				thisThreadsBookmark = true;
			}

			if (thisThreadsBookmark) {
				buffer.append("<div class=\"thisthread\">");
			} else {
				buffer.append("<div class=\"bookmark\">");
			}

			if (displayDeletionInfo) {
				buffer.append("<input type=\"checkbox\" name=\"checkbox\" value=\"" + Bookmarks.getKey(bookmark.getBoardId(), bookmark.getThreadId()) + "\"/>");
			}

			buffer.append("<a href=\"");
			buffer.append(Configuration.getInstance().getRoot());
			buffer.append("/ThreadSelectedControllerServlet?boardid=");
			buffer.append(bookmark.getBoardId());
			buffer.append("&amp;threadid=");
			buffer.append(bookmark.getThreadId());
			buffer.append("&amp;action=showall");
			buffer.append("&amp;bookmarkedmessageid=");
			buffer.append(bookmark.getMessageId());
			buffer.append("\">");

			buffer.append(bookmark.getTitle());
			buffer.append("</a>");

			if (bookmark.isUpdated()) {
				bookmarksWithUpdates++;
				buffer.append("<span class='highlightupdatedbookmark'> has new answers</span>");
			}

			buffer.append("</div>");
		}

		if (!bookmarks.isEmpty() && (displayDeletionInfo)) {
			buffer.append("<p><input type='submit' value='Remove Selected Bookmarks' /></p>");
		}

		if (bookmarks.isEmpty()) {
			buffer.append("<p><strong>You currently have no bookmarks set</strong></p>");
		}

		if (displayDeletionInfo) {
			buffer.append("</form>");
		}

		buffer.append("</div>");

		/* If there was only one bookmark with new answers, then surround the whole thing with a 'thisthread' div, so we
		 * can make it disappear with Javascript */

		if (bookmarksWithUpdates == 1) {
			buffer.insert(0, "<div class=\"thisthread\">");
			buffer.append("</div>");
		}

		return buffer.toString();
	}

}
